package 寒假刷题;

public class 推多米诺 {
    public String pushDominoes(String dominoes) {
        int length = dominoes.length();
        StringBuilder sb = new StringBuilder(dominoes);
        while (true){
            //记录当前遍历是否有多米诺变化, 没有则结束循环
            boolean hasMove = false;
            //上一张多米诺状态
            char pre = '.';
            for (int i = 0; i < length; i++) {
                //当前位置
                char cur = sb.charAt(i);
                if (cur != '.'){
                    pre = cur;
                    continue;
                }
                //下一个位置
                char next = i == length - 1 ? '.' : sb.charAt(i + 1);
                if (pre == 'R' && next != 'L'){
                    //左侧为R, 右侧为R或竖立, 则向右倒
                    hasMove = true;
                    sb.setCharAt(i, 'R');
                } else if (next == 'L' && pre != 'R'){
                    //右侧为L, 左侧为L或竖立, 则向左倒
                    hasMove = true;
                    sb.setCharAt(i, 'L');
                }
                pre = cur;
            }
            //没有多米诺变化, 结束循环
            if (!hasMove){
                break;
            }
        }
        return sb.toString();
    }
}
